package com.bhs.sansong.ui.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;


import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.bean.FaceBean;
import com.bhs.sansong.bean.FaceData;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.ToastUtil;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 选择头像
 */
public class BottomSelectiveFaceDialog extends Dialog implements View.OnClickListener, NetCallBack {

    private BaseActivity activity;
    private ImageView mIvClose;
    private RecyclerView mRvList;
    private BaseQuickAdapter<FaceBean, BaseViewHolder> mAdapter;


    public BottomSelectiveFaceDialog(@NonNull BaseActivity context) {
        super(context, R.style.MyBottomDialogStyle);
        activity = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_bottom_select_face);
        Window window = getWindow();
        //底部弹出的Dialog
        window.setGravity(Gravity.BOTTOM);
        //设置动画
        window.setWindowAnimations(R.style.main_menu_animstyle);
        WindowManager.LayoutParams attributes = window.getAttributes();
        //保证按钮水平满屏
        attributes.width = ViewGroup.LayoutParams.MATCH_PARENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        //设置显示位置
        window.setAttributes(attributes); //将属性设置给窗体
        initView();
    }

    private void initView() {
        mIvClose = findViewById(R.id.iv_close);
        mRvList = findViewById(R.id.rv_list);
        mIvClose.setOnClickListener(this);
        mAdapter=new BaseQuickAdapter<FaceBean, BaseViewHolder>(R.layout.item_face) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, FaceBean item) {
                Glide.with(getContext()).load(item.getPath()).into((ImageView) helper.getView(R.id.iv_face));
                helper.setGone(R.id.iv_check,item.isChecked());
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new GridLayoutManager(getContext(),3));
        activity.networkRequest(activity.netApi.get_default_headerpic_list("cargo"),this);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (FaceBean datum : mAdapter.getData()) {
                    datum.setChecked(false);
                }
                mAdapter.getData().get(position).setChecked(true);
                mAdapter.notifyDataSetChanged();
                myClickListener.onClick(null,mAdapter.getData().get(position).getPath());
                dismiss();
            }
        });
    }

    private MyClickListener2 myClickListener;
    public void setMyClickListener(@Nullable MyClickListener2 l) {
        this.myClickListener = l;
    }

    @Override
    public void onClick(View view) {
        dismiss();
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        FaceData data=new Gson().fromJson(json,FaceData.class);
        if(data.isStatus()){
            mAdapter.setNewData(data.getData().getList());
        }else {
            ToastUtil.show(data.getMsg());
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}
